package org.osomit.sacct.remoting.socket;

import org.osomit.sacct.service.iface.GuiceAccountServerService;
import org.osomit.sacct.service.handler.iface.ServiceHandler;
import org.osomit.sacct.entity.*;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import java.util.List;
import java.util.ArrayList;

import com.google.inject.Inject;

/**
 * 
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jun 27, 2009
 *
 */
public class SocketServiceHandler implements ServiceHandler {
    public static final String UNKNOWN_COMMAND = "unknownCommand";
    public static final String NULL_REQUEST = "emptyRequest";

    private GuiceAccountServerService serverService;

    public GuiceAccountServerService getService() {
        return serverService;
    }

    private Log log = LogFactory.getLog(SocketServiceHandler.class);
    
    @Inject
    public void setService(GuiceAccountServerService serverService) {
        this.serverService = serverService;
    }

    public Response process(Request request){
        Response resp = new Response();
//TODO: should capture all exceptions and convert them into response errorcode
        if (request != null) {
            String cmd = request.getCommand();
            RequestContext context = request.getRequestContext();
            List list = request.getParameters();
            List returnValue = new ArrayList();
            log.debug("Processing command " + cmd + " from client " + context.getClientId() + " with requestId " + context.getRequestId());
            try {
                if ("handshake".equals(cmd)) {
                    SessionInfo value = serverService.handshake(context, (String) list.get(0));
                    returnValue.add(value);
                    resp.setCommand(cmd);
                    resp.setSuccessful(true);
                    resp.setReturnValue(returnValue);
                } else if ("fetchAccount".equals(cmd)) {
                    ApplicationAccount accnt = serverService.fetchAccount(context, (String) list.get(0));
                    returnValue.add(accnt);
                    resp.setCommand(cmd);
                    resp.setSuccessful(true);
                    resp.setReturnValue(returnValue);
                } else if ("fetchAllAccounts".equals(cmd)) {
                    List<ApplicationAccount> accnts = serverService.fetchAllAccounts(context);
                    returnValue.addAll(accnts);
                    resp.setCommand(cmd);
                    resp.setSuccessful(true);
                    resp.setReturnValue(returnValue);
                } else if ("hello".equals(cmd)) {
                    String value = serverService.hello(context);
                    returnValue.add(value);
                    resp.setCommand(cmd);
                    resp.setSuccessful(true);
                    resp.setReturnValue(returnValue);
                } else if ("closeSession".equals(cmd)){
                    String value = serverService.closeSession(context, (String) list.get(0));
                    returnValue.add(value);
                    resp.setCommand(cmd);
                    resp.setSuccessful(true);
                    resp.setReturnValue(returnValue);                    
                } else {
                    resp.setCommand(cmd);
                    resp.setSuccessful(false);
                    resp.setErrorCode(UNKNOWN_COMMAND);
                }
            } catch (Exception e) {
                log.error("Error processing command " + cmd + " with exception: " + e.getMessage());
                resp.setCommand(cmd);
                resp.setSuccessful(false);
                resp.setErrorCode(e.getMessage());
            }
        }else{
            resp.setSuccessful(false);
            resp.setErrorCode(NULL_REQUEST);
        }

        return resp;
    }
}
